import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:untitled1/app/broker/modules/serve/ServePage.dart';
import 'package:untitled1/app/broker/modules/my/MyPage.dart';
import 'package:untitled1/app/res/myColors.dart';
import 'package:untitled1/app/utils/utilDialog.dart';

import '../../../utils/utilScreen.dart';
import '../../../widgets/dialog/MyBottomDialog.dart';
import '../employer/EmployerPage.dart';
import '../employer/employer_page/employer_page_dialog/EmployerPage_citySelect.dart';
import '../order/OrderPage.dart';
import 'HomePage.dart';

//主页
class AppHomePage extends StatefulWidget {
  @override
  State<AppHomePage> createState() => _JingJiRenHomePageState();
}

class _JingJiRenHomePageState extends State<AppHomePage> {
  int _currentIndex = 2;

  //PageView子页面
  List<Widget> _pages = [
    EmployerPage(),
    OrderPage(),
    HomePage(),
    ServePage(),
    MyPage(),
  ];

  //PageView控制器
  late PageController _controller;

  @override
  void initState() {
    super.initState();
    //设置当前的页面索引
    _controller = PageController(
      initialPage: _currentIndex, //设置页面索引， 显示第几个页面
      viewportFraction: 1.0, // 每页占满整个视口（可设置为 0.8 实现左右预览）
    );
    // 延迟到第一帧绘制完成后弹出对话框
    WidgetsBinding.instance.addPostFrameCallback((_) {
      // dialog_citySelect();
      // CitySelectDialog.dialog_citySelect(context,(data){});
    });
  }

  @override
  Widget build(BuildContext context) {
    // showDialog();
    return Scaffold(
      body: Container(
        width: utilScreen.getScreenWidth(context),
        height: utilScreen.getScreenHeight(context),
        color: myColors.c_D1D3D5,
        child: PageView.builder(
          //viepage
          scrollDirection: Axis.horizontal,
          //滚动方向，默认横向滚动
          physics: NeverScrollableScrollPhysics(),
          //viewPage禁止左右滑动
          onPageChanged: (int index) {
            //监听每次滑动
            print('>]onPageChanged');
            if (index != _currentIndex) {
              //改变当前索引，刷新页面，同时改变底部导航栏的选中状态
              _currentIndex = index;
              //刷新页面
              setState(() {});
            }
          },
          controller: _controller,
          //设置控制器
          itemCount: _pages.length,
          //设置页面数量
          //也可以这样写itemBuilder: (context, index) => _pages[index]
          //构建每个页面内容
          itemBuilder: (context, index) {
            //如果相同页面可以直接写页面，如：return Page("第一页1111")
            return _pages[index]; //返回具体的页面
          },
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.white,
        type: BottomNavigationBarType.fixed,
        fixedColor: Color(0xffFF4142),
        //选中时颜色
        unselectedItemColor: myColors.c_191919,
        //未选中时颜色
        currentIndex: _currentIndex,
        //当前选中的索引
        onTap: (int index) {
          _currentIndex = index;
          print('>]点击事件');
          //点击底部导航栏跳转响应的页面
          _controller.jumpToPage(_currentIndex);
          setState(() {});
        },
        //点击事件
        items: [
          BottomNavigationBarItem(
            icon: Image.asset(
              width: 20,
              height: 20,
              _currentIndex == 0
                  ? 'assets/app/bnt_item1.png'
                  : 'assets/app/bnt_item1_2.png',
            ),
            label: "雇主",
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              width: 20,
              height: 20,
              _currentIndex == 1
                  ? 'assets/app/bnt_item2.png'
                  : 'assets/app/bnt_item2_2.png',
            ),
            label: "订单",
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              width: 20,
              height: 20,
              _currentIndex == 2
                  ? 'assets/app/bnt_item3.png'
                  : 'assets/app/bnt_item3_2.png',
            ),
            label: "首页",
          ),
          BottomNavigationBarItem(
              icon: Image.asset(
                width: 20,
                height: 20,
                _currentIndex == 3
                    ? 'assets/app/bnt_item4.png'
                    : 'assets/app/bnt_item4_2.png',
              )
              , label: "家员"),
          BottomNavigationBarItem(
              icon: Image.asset(
                width: 20,
                height: 20,
                _currentIndex == 4
                    ? 'assets/app/bnt_item5.png'
                    : 'assets/app/bnt_item5_2.png',
              )
          , label: "我的"),
        ],
      ),
    );
  }

  void dialog_citySelect() {
    MyBottomDialog.show(
      context: context,
      width: double.infinity,
      // 👈 固定宽高
      height: double.infinity,
      // 👈 高度可自定义或用 double.infinity
      borderRadius: 0,
      // 👈 可设置为 0 取消圆角
      animationType: DialogAnimationType.popup,
      //底部划出
      builder: (ctx) => EmployerPage_citySelect(onConfirm: (data) {}),
    );
  }
}
